 package uk.co.mmscomputing.concurrent;
 
 public class Timer extends Semaphore
 {
   private boolean waiting;
   protected int timeout;
   protected int delay;
   protected TimerListener listener;
 
   public Timer(int paramInt)
   {
     super(0, true);
     this.timeout = paramInt;
   }
   public void setDelay(int paramInt) {
     this.delay = paramInt; } 
   public void setListener(TimerListener paramTimerListener) { this.listener = paramTimerListener; }
 
   public void acquire() throws InterruptedException {
     this.waiting = true;
     new CountdownThread().start();
     super.acquire();
     this.waiting = false;
   }
 
   public boolean tryAcquire(long paramLong, TimeUnit paramTimeUnit) throws InterruptedException {
     this.waiting = true;
     new CountdownThread().start();
     boolean bool = super.tryAcquire(paramLong, paramTimeUnit);
     this.waiting = false;
     return bool;
   }
   class CountdownThread extends Thread {
     CountdownThread() {
     }
     public void run() { int i = Timer.this.timeout;
 
       Timer.this.listener.begin(i);
       while (Timer.this.waiting)
         try {
           i -= Timer.this.delay;
           sleep(Timer.this.delay);
           Timer.this.listener.tick(i);
         } catch (InterruptedException localInterruptedException) {
         }
       Timer.this.listener.end(i);
     }
   }
 }

